CMAKE_MINIMUM_REQUIRED(VERSION 3.17)

IF (CMAKE_SYSTEM MATCHES "Windows")
    IF (NOT TARGET MIMALLOC::H)
        ADD_LIBRARY(MIMALLOC::H INTERFACE IMPORTED)
        TARGET_INCLUDE_DIRECTORIES(MIMALLOC::H INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/windows/include")
    ENDIF ()

    IF (NOT TARGET MIMALLOC::LIB)
        FIND_LIBRARY(MIMALLOC_LIB NAMES mimalloc-static PATHS ${CMAKE_CURRENT_SOURCE_DIR}/windows/lib NO_DEFAULT_PATH)
        ADD_LIBRARY(MIMALLOC::LIB STATIC IMPORTED)
        SET_TARGET_PROPERTIES(MIMALLOC::LIB PROPERTIES IMPORTED_LOCATION "${MIMALLOC_LIB}")
    ENDIF ()
ELSEIF (CMAKE_SYSTEM MATCHES "Linux")
    IF (NOT TARGET MIMALLOC::H)
        ADD_LIBRARY(MIMALLOC::H INTERFACE IMPORTED)
        TARGET_INCLUDE_DIRECTORIES(MIMALLOC::H INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/ubuntu/include")
    ENDIF ()

    IF (NOT TARGET MIMALLOC::LIB)
        FIND_LIBRARY(MIMALLOC_LIB NAMES mimalloc PATHS ${CMAKE_CURRENT_SOURCE_DIR}/ubuntu/lib NO_DEFAULT_PATH)
        ADD_LIBRARY(MIMALLOC::LIB STATIC IMPORTED)
        SET_TARGET_PROPERTIES(MIMALLOC::LIB PROPERTIES IMPORTED_LOCATION "${MIMALLOC_LIB}")
    ENDIF ()
ENDIF ()

IF (NOT TARGET LIBRARIES::MIMALLOC)
    ADD_LIBRARY(LIBRARIES::MIMALLOC INTERFACE IMPORTED GLOBAL)
    TARGET_LINK_LIBRARIES(LIBRARIES::MIMALLOC INTERFACE
            MIMALLOC::H
            MIMALLOC::LIB)
ENDIF ()